﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ArivisTests.Models;

namespace ArivisTests.Controllers
{
    [Authorize]
    public class TestScenarioController : Controller
    {
        private ArivisTestsDBContext db = new ArivisTestsDBContext();

        //
        // GET: /TestScenario/

        public ActionResult Index(int? page)
        {
            int pageSize = 20;
            int testCount = db.TestScenarios.Count();
            int pageCount = testCount / pageSize;
            if (testCount % pageSize > 0) pageCount++;

            ViewBag.Page = page.GetValueOrDefault();
            ViewBag.PageSize = pageSize;
            ViewBag.PageCount = pageCount;
            ViewBag.TestCount = testCount;
            
            int skip = page.GetValueOrDefault() * pageSize;
            return View(db.TestScenariosWithOwners.OrderBy(x => x.Id).Skip(skip).Take(pageSize));
        }

        //
        // GET: /TestScenario/Details/5

        public ActionResult Details(int id = 0)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            if (testscenario == null)
            {
                return HttpNotFound();
            }
            ViewBag.db = db;
            return View(testscenario);
        }

        //
        // GET: /TestScenario/Create

        public ActionResult Create()
        {
            ViewBag.db = db;
            return View(new TestScenario { Duration = 60 });
        }

        //
        // POST: /TestScenario/Create

        [HttpPost]
        public ActionResult Create(TestScenario testscenario, int? userId)
        {
            if (ModelState.IsValid)
            {
                db.TestScenarios.Add(testscenario);
                testscenario.Owner = db.UserProfiles.FirstOrDefault(x => x.UserId == userId);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.db = db;
            return View(testscenario);
        }

        //
        // GET: /TestScenario/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            if (testscenario == null)
            {
                return HttpNotFound();
            }
            ViewBag.db = db;
            return View(testscenario);
        }

        //
        // POST: /TestScenario/Edit/5

        [HttpPost]
        public ActionResult Edit(TestScenario testscenario, int? userId)
        {
            if (ModelState.IsValid)
            {
                var dbScenario = db.TestScenariosWithOwners.First(x => x.Id == testscenario.Id);
                dbScenario.Name = testscenario.Name;
                dbScenario.Duration = testscenario.Duration;
                dbScenario.Url = testscenario.Url;
                dbScenario.Owner = db.UserProfiles.FirstOrDefault(x => x.UserId == userId);
                dbScenario.TicketUrl = testscenario.TicketUrl;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.db = db;
            return View(testscenario);
        }

        //
        // GET: /TestScenario/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            if (testscenario == null)
            {
                return HttpNotFound();
            }
            return View(testscenario);
        }

        //
        // POST: /TestScenario/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            db.TestScenarios.Remove(testscenario);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}